package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrCodeBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.MatrCodeMapper;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 物料编码-添加
 *
 * @author 廖大富
 */
public class MatrCodeAdd implements ServerInterface {

    private static Logger logger = LogManager.getLogger(MatrCodeAdd.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            MatrCodeMapper mapper = factory.getMapper(MatrCodeMapper.class);
            MatrCodeBean bean = JsonUtils.objectToPojo(req.getData(), MatrCodeBean.class);
            // 校验字段重复
            if (mapper.check("matr_no", bean.getMatrNo()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "材料编号：" + bean.getMatrNo() + "的信息已存在。", void.class);
            }
            if (mapper.check("matr_name", bean.getMatrName()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "材料名称：" + bean.getMatrName() + "的信息已存在。", void.class);
            }
            bean.setCreateInfo(userInfo);
            handCode(mapper, bean);
            mapper.insertMatrCode(bean);
        } catch (Exception e) {
            logger.error("物料编码添加失败", e);
            factory.close(false);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "物料编码添加失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }

    /**
     * 自动生成层级code
     *
     * @param mapper
     * @param bean
     */
    public static void handCode(MatrCodeMapper mapper, MatrCodeBean bean) {
        // 生成层级code, 规则5位一级，下级自动继承上级
        String parentClassCode = bean.getClassCode();
        MatrCodeBean parentBean = null;
        if (StringUtils.isNotBlank(bean.getParentId()) && !bean.getParentId().equals("0")) {
            parentBean = mapper.findMatrCodeById(bean.getParentId()); // 通过id查询数据
            parentClassCode = parentBean.getClassCode();
        }
        // 取父级目录下最大的下级目录
        String maxCode = mapper.findMaxChild(parentClassCode);// 通过层级编号查询
        if (StringUtils.isBlank(maxCode)) {// 如果未找到，说明下面没有子级，给初始值
            maxCode = parentClassCode + "00000";
        }
        // 未位加1
        long last5 = Long.parseLong("1" + maxCode.substring(maxCode.length() - 5)) + 1;
        maxCode = maxCode.substring(0, maxCode.length() - 5) + (last5 + "").substring(1);
        // 重设code
        bean.setClassCode(maxCode);
        bean.setIsLeaf(1);// 新增加的数据，都是叶结点
        // 自动更新其上级为目录
        if (parentBean != null) {
            parentBean.setIsLeaf(0);
            parentBean.setUpdateTime(CommonUtils.getCurrentTime());
            mapper.updateMatrCode(parentBean);
        }
    }
}
